package cn.com.winning.ecare.utils;

public class StringUtil
{

	/**
	 * 比较两个String
	 * 
	 * @param actual
	 * @param expected
	 * @return <ul>
	 *         <li>若两个字符串都为null，则返回true</li>
	 *         <li>若两个字符串都不为null，且相等，则返回true</li>
	 *         <li>否则返回false</li>
	 *         </ul>
	 */
	public static boolean isEquals(String actual, String expected)
	{
		return ObjectUtils.isEquals(actual, expected);
	}

	/**
	 * null字符串转换为长度为0的字符串
	 * 
	 * @param str
	 *        待转换字符串
	 * @return
	 * @see <pre>
	 * nullStrToEmpty(null) = &quot;&quot;;
	 * nullStrToEmpty(&quot;&quot;) = &quot;&quot;;
	 * nullStrToEmpty(&quot;aa&quot;) = &quot;aa&quot;;
	 * </pre>
	 */
	public static String nullStrToEmpty(String str)
	{
		return (str == null ? "" : str);
	}

	/**
	 * 将字符串首字母大写后返回
	 * 
	 * @param str
	 *        原字符串
	 * @return 首字母大写后的字符串
	 * 
	 *         <pre>
	 *      capitalizeFirstLetter(null)     =   null;
	 *      capitalizeFirstLetter("")       =   "";
	 *      capitalizeFirstLetter("2ab")    =   "2ab"
	 *      capitalizeFirstLetter("a")      =   "A"
	 *      capitalizeFirstLetter("ab")     =   "Ab"
	 *      capitalizeFirstLetter("Abc")    =   "Abc"
	 * </pre>
	 */
	public static String capitalizeFirstLetter(String str)
	{
		if (isEmpty(str))
		{
			return str;
		}

		char c = str.charAt(0);
		return (!Character.isLetter(c) || Character.isUpperCase(c)) ? str : new StringBuilder(str.length())
				.append(Character.toUpperCase(c)).append(str.substring(1)).toString();
	}

	/**
	 * 判断字符串是否是整数
	 * 
	 * @param number
	 * @return
	 */
	public static boolean isInteger(String number)
	{
		boolean isNumber = false;
		if (StringUtil.isNotEmpty(number))
		{
			isNumber = number.matches("^([1-9]\\d*)|(0)$");
		}
		return isNumber;
	}

	/**
	 * 判断字符串不为空
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isNotEmpty(String str)
	{
		boolean isNotEmpty = false;
		if (str != null && !str.trim().equals("") && !str.trim().equalsIgnoreCase("NULL"))
		{
			isNotEmpty = true;
		}
		return isNotEmpty;
	}

	/**
	 * 判断字符串为空
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isEmpty(String str)
	{
		return !isNotEmpty(str);
	}
	
	/**
	 * 判断字符串不为空
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isObjNotEmpty(Object obj)
	{
		boolean isNotEmpty = false;
		if (obj != null)
		{
			isNotEmpty = true;
		}
		return isNotEmpty;
	}

	/**
	 * 判断字符串为空
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isObjEmpty(Object obj)
	{
		return !isObjNotEmpty(obj);
	}

	/**
	 * 将数组转成SQL认识的字符串 123,432,2312 id in('123','432','2312')
	 * 
	 * @param ids
	 * @return
	 */
	public static String fromArrayToStr(String[] ids)
	{
		StringBuffer str = new StringBuffer();
		for (int i = 0; i < ids.length; i++)
		{
			str.append("'" + ids[i] + "',");
		}
		if (ids.length > 0)
		{
			str.deleteCharAt(str.length() - 1);
		}
		return str.toString();
	}
}
